package com.hei.controller;

import com.hei.pojo.Barticle;
import com.hei.pojo.Bkind;
import com.hei.pojo.Blabel;
import com.hei.pojo.Buser;
import com.hei.pojo.dto.Manage;
import com.hei.pojo.dto.ManageArticle;
import com.hei.service.BarticleService;
import com.hei.service.BcommentService;
import com.hei.service.BkindService;
import com.hei.service.BlabelService;
import com.hei.util.uuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private BarticleService barticleService;

    @Autowired
    private BcommentService bcommentService;

    @Autowired
    private BlabelService blabelService;

    @Autowired
    BkindService bkindService;

    @RequestMapping("/manage")
    public String toManage(Model model){
        int pca = barticleService.countPublishArticle();//发表文章数
        Map<String,Object> map=new HashMap<>();
        map.put("publish","1");
        int ac = bcommentService.queryAllCount();//评论总数
        List<Blabel> labels = blabelService.queryAllLabel(map);
        List<Bkind> kinds = bkindService.queryAllKind(map);
        List<ManageArticle> articles = barticleService.queryManageArticle();
        Manage manage=new Manage(pca,articles.size(),ac,labels.size(),kinds.size());
        manage.setArticles(articles);
        manage.setKinds(kinds);
        manage.setLabels(labels);
        model.addAttribute("manage",manage);
        return "manage";
    }


    @GetMapping("/edit/{id}")
    public String updateArticle(@PathVariable("id")String id,Model model){
        Barticle article = barticleService.selectArticleById(id);
        model.addAttribute("article",article);
        return "edit";
    }
    @PostMapping("/publishArticle/{id}")
    public String publishArticle(@PathVariable("id") String id){
        barticleService.updatePublish(id);
        return "redirect:/manage/manage";
    }

    @PostMapping("/unpublishArticle/{id}")
    public String unpublishArticle(@PathVariable("id") String id){
        barticleService.updateUnpublish(id);
        return "redirect:/manage/manage";
    }

    @PostMapping("/deleteArticle/{id}")
    public String deleteArticle(@PathVariable("id") String id){
        barticleService.deleteArticle(id);
        return "redirect:/manage/manage";
    }

    @PostMapping("/addLabel")
    public String addLabel(Blabel label, HttpSession session){
        Buser user = (Buser) session.getAttribute("user");
        label.setuId(user.getId());
        blabelService.addLabel(label);
        return "redirect:/manage/manage";
    }

    @PostMapping("/deleteLabel")
    public String deleteLabel(String id,Model model){
        Blabel blabel = blabelService.queryById(id);
        if(StringUtils.isEmpty(blabel)){
            model.addAttribute("labelerror","没有此id");
            return "redirect:/manage/manage";
        }
        blabelService.deleteLabel(id);
        return "redirect:/manage/manage";
    }

    @PostMapping("/addKind")
    public String addKind(Bkind kind,HttpSession session){
        Buser user = (Buser) session.getAttribute("user");
        kind.setuId(user.getId());
        bkindService.addKind(kind);
        return "redirect:/manage";
    }

    @PostMapping("/deleteKind")
    public String deleteKind(String id){
        bkindService.deleteKind(id);
        return "redirect:/manage";
    }


}
